/*
 * @(#)MtmUserRecordUserRole.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.user.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.user.bean.MtmUserGroupUserRole;
import com.dcivision.user.bean.MtmUserRecordUserRole;
import com.dcivision.user.bean.UserRole;

/**
  MtmUserRecordUserRoleDAObject.java

  This class is the data access bean for table "MTM_USER_RECORD_USER_ROLE".

  @author      Rollo Chan
  @company     DCIVision Limited
  @creation date   17/07/2003
  @version     $Revision: 1.24 $
*/

public class MtmUserRecordUserRoleDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.24 $";

  public static final String TABLE_NAME = "MTM_USER_RECORD_USER_ROLE";

  public MtmUserRecordUserRoleDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
    this.isSoftDeleteEnabled = false;
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("USER_RECORD_ID");
    this.vecDBColumn.add("USER_ROLE_ID");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.USER_ROLE_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_USER_RECORD_USER_ROLE A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          MtmUserRecordUserRole tmpMtmUserRecordUserRole = new MtmUserRecordUserRole();
          tmpMtmUserRecordUserRole.setID(getResultSetInteger(rs, "ID"));
          tmpMtmUserRecordUserRole.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpMtmUserRecordUserRole.setUserRoleID(getResultSetInteger(rs, "USER_ROLE_ID"));
          tmpMtmUserRecordUserRole.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmUserRecordUserRole.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmUserRecordUserRole.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmUserRecordUserRole.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmUserRecordUserRole.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmUserRecordUserRole.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmUserRecordUserRole.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmUserRecordUserRole.getCreatorID()));
          tmpMtmUserRecordUserRole.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmUserRecordUserRole.getUpdaterID()));
          return(tmpMtmUserRecordUserRole);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.USER_ROLE_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_USER_RECORD_USER_ROLE A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          MtmUserRecordUserRole tmpMtmUserRecordUserRole = new MtmUserRecordUserRole();
          tmpMtmUserRecordUserRole.setID(getResultSetInteger(rs, "ID"));
          tmpMtmUserRecordUserRole.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpMtmUserRecordUserRole.setUserRoleID(getResultSetInteger(rs, "USER_ROLE_ID"));
          tmpMtmUserRecordUserRole.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmUserRecordUserRole.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmUserRecordUserRole.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmUserRecordUserRole.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmUserRecordUserRole.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmUserRecordUserRole.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmUserRecordUserRole.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmUserRecordUserRole.getCreatorID()));
          tmpMtmUserRecordUserRole.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmUserRecordUserRole.getUpdaterID()));
          tmpMtmUserRecordUserRole.setRecordCount(totalNumOfRecord);
          tmpMtmUserRecordUserRole.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpMtmUserRecordUserRole);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.USER_ROLE_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_USER_RECORD_USER_ROLE A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmUserRecordUserRole tmpMtmUserRecordUserRole = new MtmUserRecordUserRole();
          tmpMtmUserRecordUserRole.setID(getResultSetInteger(rs, "ID"));
          tmpMtmUserRecordUserRole.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpMtmUserRecordUserRole.setUserRoleID(getResultSetInteger(rs, "USER_ROLE_ID"));
          tmpMtmUserRecordUserRole.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmUserRecordUserRole.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmUserRecordUserRole.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmUserRecordUserRole.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmUserRecordUserRole.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmUserRecordUserRole.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmUserRecordUserRole.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmUserRecordUserRole.getCreatorID()));
          tmpMtmUserRecordUserRole.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmUserRecordUserRole.getUpdaterID()));
          result.add(tmpMtmUserRecordUserRole);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmUserRecordUserRole tmpMtmUserRecordUserRole = (MtmUserRecordUserRole)((MtmUserRecordUserRole)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   MTM_USER_RECORD_USER_ROLE(ID, USER_RECORD_ID, USER_ROLE_ID, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpMtmUserRecordUserRole.getUserRecordID());
        setPrepareStatement(preStat, 3, tmpMtmUserRecordUserRole.getUserRoleID());
        setPrepareStatement(preStat, 4, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 5, new Integer(0));
        setPrepareStatement(preStat, 6, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 7, currTime);
        setPrepareStatement(preStat, 8, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 9, currTime);
        preStat.executeUpdate();
        tmpMtmUserRecordUserRole.setID(nextID);
        tmpMtmUserRecordUserRole.setCreatorID(sessionContainer.getUserRecordID());
        tmpMtmUserRecordUserRole.setCreateDate(currTime);
        tmpMtmUserRecordUserRole.setUpdaterID(sessionContainer.getUserRecordID());
        tmpMtmUserRecordUserRole.setUpdateDate(currTime);
        tmpMtmUserRecordUserRole.setUpdateCount(new Integer(0));
        tmpMtmUserRecordUserRole.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmUserRecordUserRole.getCreatorID()));
        tmpMtmUserRecordUserRole.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmUserRecordUserRole.getUpdaterID()));
        return(tmpMtmUserRecordUserRole);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmUserRecordUserRole tmpMtmUserRecordUserRole = (MtmUserRecordUserRole)((MtmUserRecordUserRole)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE MTM_USER_RECORD_USER_ROLE ");
        sqlStat.append("SET  USER_RECORD_ID=?, USER_ROLE_ID=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmUserRecordUserRole.getUserRecordID());
        setPrepareStatement(preStat, 2, tmpMtmUserRecordUserRole.getUserRoleID());
        setPrepareStatement(preStat, 3, new Integer(tmpMtmUserRecordUserRole.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 4, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 5, currTime);
        setPrepareStatement(preStat, 6, tmpMtmUserRecordUserRole.getID());
        setPrepareStatement(preStat, 7, tmpMtmUserRecordUserRole.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpMtmUserRecordUserRole.setUpdaterID(sessionContainer.getUserRecordID());
          tmpMtmUserRecordUserRole.setUpdateDate(currTime);
          tmpMtmUserRecordUserRole.setUpdateCount(new Integer(tmpMtmUserRecordUserRole.getUpdateCount().intValue() + 1));
          tmpMtmUserRecordUserRole.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmUserRecordUserRole.getCreatorID()));
          tmpMtmUserRecordUserRole.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmUserRecordUserRole.getUpdaterID()));
          return(tmpMtmUserRecordUserRole);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmUserRecordUserRole tmpMtmUserRecordUserRole = (MtmUserRecordUserRole)((MtmUserRecordUserRole)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_USER_RECORD_USER_ROLE ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmUserRecordUserRole.getID());
        setPrepareStatement(preStat, 2, tmpMtmUserRecordUserRole.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpMtmUserRecordUserRole);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    MtmUserRecordUserRole tmpMtmUserRecordUserRole = (MtmUserRecordUserRole)this.oldValue;

    if (tmpMtmUserRecordUserRole != null) {
      oldValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getID()));
      oldValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getUserRecordID()));
      oldValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getUserRoleID()));
      oldValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getUpdateDate()));
    }

    tmpMtmUserRecordUserRole = (MtmUserRecordUserRole)obj;
    if (tmpMtmUserRecordUserRole != null) {
      newValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getID()));
      newValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getUserRecordID()));
      newValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getUserRoleID()));
      newValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpMtmUserRecordUserRole.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
 
  public synchronized void deleteListByUserRecordID(Integer userRecordID) throws ApplicationException {
    this.deleteListByUserRecordID(userRecordID,null);
  }
  
  public synchronized void deleteListByUserRecordID(Integer userRecordID,Integer userRoleID) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_USER_RECORD_USER_ROLE ");
        sqlStat.append("WHERE  USER_RECORD_ID=? ");
        if(!Utility.isEmpty(userRoleID)){
          sqlStat.append("AND USER_ROLE_ID=? ");   
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, userRecordID);
        if(!Utility.isEmpty(userRoleID)){
          setPrepareStatement(preStat, 2, userRoleID);   
        }
        updateCnt = preStat.executeUpdate();
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized void deleteListByUserRoleID(Integer userRoleID) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_USER_RECORD_USER_ROLE ");
        sqlStat.append("WHERE  USER_ROLE_ID=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, userRoleID);
        updateCnt = preStat.executeUpdate();
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public List getListByUserRoleAry(List roleList) throws ApplicationException{
    return this.getListByUserRoleAry(roleList,null); 
  }
  
  public synchronized List getListByUserRoleAry(List roleList,String recordStatus) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
        try {
            sqlStat.append("SELECT ID, USER_RECORD_ID, USER_ROLE_ID, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE ");
            sqlStat.append("FROM   MTM_USER_RECORD_USER_ROLE ");
            for (int i=0; i<roleList.size();i++) {
              if(i==0) {
                sqlStat.append("WHERE  (USER_ROLE_ID = ?) ");
              } else {
                sqlStat.append("OR  (USER_ROLE_ID = ?) ");
              }
            }
            if(!Utility.isEmpty(recordStatus)){
              sqlStat.append("AND (RECORD_STATUS=?) ");   
            }
            preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            int j=0;
            for (int i=0; i<roleList.size();i++) {  
              j+=1;
              this.setPrepareStatement(preStat, j, (Integer)roleList.get(i));
            }
            if(!Utility.isEmpty(recordStatus)){
              this.setPrepareStatement(preStat,j+1,recordStatus);   
            }
            rs = preStat.executeQuery();
            while (rs.next()) {
                MtmUserRecordUserRole tmpMtmUserRecordUserRole = new MtmUserRecordUserRole();
                tmpMtmUserRecordUserRole.setID(getResultSetInteger(rs, "ID"));
                tmpMtmUserRecordUserRole.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
                tmpMtmUserRecordUserRole.setUserRoleID(getResultSetInteger(rs, "USER_ROLE_ID"));
                tmpMtmUserRecordUserRole.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
                tmpMtmUserRecordUserRole.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
                tmpMtmUserRecordUserRole.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
                tmpMtmUserRecordUserRole.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
                result.add(tmpMtmUserRecordUserRole);
            }
            return(result);
        } catch (ApplicationException appEx) {
            throw appEx;
        } catch (SQLException sqle) {
            log.error(sqle, sqle);
            throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle);
        } catch (Exception e) {
            log.error(e, e);
            throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
        } finally {
            try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
            try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        }
    }
  }

  public synchronized List getUserRoleListByUserRecordID(Integer userRecordID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    UserRoleDAObject userRoleDAO = new UserRoleDAObject(sessionContainer, dbConn);

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT USER_ROLE_ID ");
        sqlStat.append("FROM   MTM_USER_RECORD_USER_ROLE A, USER_ROLE B ");
        sqlStat.append("WHERE  A.USER_ROLE_ID = B.ID AND A.USER_RECORD_ID=? AND A.RECORD_STATUS=? AND B.RECORD_STATUS=? ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, userRecordID);
        setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 3, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();

        while (rs.next()) {
          UserRole tmpUserRole = (UserRole)userRoleDAO.getObjectByID(getResultSetInteger(rs, "USER_ROLE_ID"));
          result.add(tmpUserRole);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public synchronized List getListByUserRecordID(Integer userID) throws ApplicationException {
    return this.getListByUserRecordID(userID,null); 
  }

  public synchronized List getListByUserRecordID(Integer userID,Integer userRoleID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized (dbConn) {
      try {
        sqlStat.append("SELECT ID, USER_RECORD_ID, USER_ROLE_ID, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE, RECORD_STATUS ");
        sqlStat.append("FROM   MTM_USER_RECORD_USER_ROLE ");
        sqlStat.append("WHERE  USER_RECORD_ID = ? AND RECORD_STATUS = ? ");
        if(!Utility.isEmpty(userRoleID)){
          sqlStat.append("AND USER_ROLE_ID=?");   
        }
        preStat = dbConn.prepareStatement(sqlStat.toString());
        this.setPrepareStatement(preStat, 1, userID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        if(!Utility.isEmpty(userRoleID)){
          this.setPrepareStatement(preStat, 3, userRoleID);   
        }
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmUserRecordUserRole tmpMtmUserRecordUserRole = new MtmUserRecordUserRole();
          tmpMtmUserRecordUserRole.setID(getResultSetInteger(rs, "ID"));
          tmpMtmUserRecordUserRole.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpMtmUserRecordUserRole.setUserRoleID(getResultSetInteger(rs, "USER_ROLE_ID"));
          tmpMtmUserRecordUserRole.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmUserRecordUserRole.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmUserRecordUserRole.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmUserRecordUserRole.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmUserRecordUserRole.setCreatorName(sessionContainer == null ? "" : UserInfoFactory.getUserFullName(tmpMtmUserRecordUserRole.getCreatorID()));
          tmpMtmUserRecordUserRole.setUpdaterName(sessionContainer == null ? "" : UserInfoFactory.getUserFullName(tmpMtmUserRecordUserRole.getUpdaterID()));
          result.add(tmpMtmUserRecordUserRole);
        }
        return (result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized List getUserIDListByUserRoleList(List roleList) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized (dbConn) {
      try {
        sqlStat.append("SELECT DISTINCT USER_RECORD_ID ");
        sqlStat.append("FROM   MTM_USER_RECORD_USER_ROLE ");
        for (int i = 0; i < roleList.size(); i++) {
          if (i == 0) {
            sqlStat.append("WHERE  (USER_ROLE_ID = ?) ");
          } else {
            sqlStat.append("OR  (USER_ROLE_ID = ?) ");
          }
        }
        preStat = dbConn.prepareStatement(sqlStat.toString());
        for (int i = 0; i < roleList.size(); i++) {
          this.setPrepareStatement(preStat, i + 1, (Integer) roleList.get(i));
        }
        rs = preStat.executeQuery();
        while (rs.next()) {
          Integer userRecordID = getResultSetInteger(rs, "USER_RECORD_ID");
          result.add( userRecordID);
        }
        return (result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  /**
   * get user ID list by user role ID
   * @param roleID
   * @return
   * @throws ApplicationException
   */
  public synchronized List getUserIDListByUserRoleID(Integer roleID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized (dbConn) {
      try {
        sqlStat.append("SELECT DISTINCT A.USER_RECORD_ID ");
        sqlStat.append("FROM   MTM_USER_RECORD_USER_ROLE A, USER_RECORD B ");
        sqlStat.append("WHERE  A.USER_ROLE_ID = ? ");
        sqlStat.append("AND    A.RECORD_STATUS = ? ");
        sqlStat.append("AND    A.USER_RECORD_ID = B.ID ");
        sqlStat.append("AND    A.RECORD_STATUS = B.RECORD_STATUS ");
         
        preStat = dbConn.prepareStatement(sqlStat.toString());
        this.setPrepareStatement(preStat, 1, roleID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        
        rs = preStat.executeQuery();
        while (rs.next()) {
          Integer userRecordID = getResultSetInteger(rs, "USER_RECORD_ID");
          result.add( userRecordID);
        }
        return (result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * get user record id list by user role id list
   * @param roleIDList
   * @return
   * @throws ApplicationException
   */
  public synchronized List getUserRecordIDListByUserRoleIDList(List roleIDList) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    UserRoleDAObject userRoleDAO = new UserRoleDAObject(sessionContainer, dbConn);

    String roleIDs = "";
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT DISTINCT A.USER_RECORD_ID ");
        sqlStat.append("FROM   MTM_USER_RECORD_USER_ROLE A ");
        sqlStat.append("WHERE  RECORD_STATUS = ? ");
         
        for (int i = 0 ; i < roleIDList.size(); i++){
          if(i != 0){
            roleIDs += ",";
          }
          roleIDs += roleIDList.get(i);  
        }

        if(!Utility.isEmpty(roleIDs)){
          sqlStat.append("AND    A.USER_ROLE_ID IN (" + roleIDs + ") ");  
        }
        
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();

        while (rs.next()) {
          result.add(getResultSetInteger(rs, "USER_RECORD_ID"));
        }
        
        
        MtmUserGroupUserRoleDAObject groupRoleDAO = new MtmUserGroupUserRoleDAObject(this.sessionContainer, this.dbConn);
        MtmUserRecordUserGroupDAObject userGroupDAO = new MtmUserRecordUserGroupDAObject(this.sessionContainer, this.dbConn);
              
        // get group ID List belond to userRoleIDList
        List tmpGroupRoleList = groupRoleDAO.getListByUserRoleAry(roleIDList, GlobalConstant.RECORD_STATUS_ACTIVE);
        List tmpGroupIDList = new ArrayList();
        for(int i = 0; i < tmpGroupRoleList.size(); i++){
          MtmUserGroupUserRole groupRole = (MtmUserGroupUserRole)tmpGroupRoleList.get(i);
          tmpGroupIDList.add(groupRole.getUserGroupID());
        }
        
        // get userRecord ID belond to group ID List
        if(!Utility.isEmpty(tmpGroupIDList)){
          List tmpUserRecordIDList = userGroupDAO.getUserIDListByUserGroupIDList(tmpGroupIDList);  
          for(int i = 0; i < tmpUserRecordIDList.size(); i++){
            if(result.indexOf(tmpUserRecordIDList.get(i)) < 0 ){
              result.add(tmpUserRecordIDList.get(i)) ; 
            }
          }
        }
        
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  
}
